//
//  NSMutableDictionary+Format.m
//  ERP
//
//  Created by goldeney on 15/6/10.
//  Copyright (c) 2015年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "NSMutableDictionary+Format.h"

@implementation NSMutableDictionary (Format)

- (id)objectFormatForKey:(id)aKey
{
    if (!aKey) return nil;
    id objectValue = [self objectForKey:aKey];
    
    //赋值给nil之外的其他空值
    if (objectValue == [NSNull null])
        return nil;
    
    if ([objectValue isKindOfClass:[NSString class]] && [objectValue isEqualToString:@""])
        return nil;
    
    return objectValue;
}

- (NSString*)objectFormatStringForKey:(id)aKey
{
    id value = [self objectFormatForKey:aKey];
    if (value) return [NSString stringWithFormat:@"%@",value];
    else return @"";
}

- (NSNumber*)objectFormatFloatNumberForKey:(id)aKey
{
    id value = [self objectFormatForKey:aKey];
    if (value && [value isKindOfClass:[NSNumber class]])
        return value;
    
    if (value && [value isKindOfClass:[NSString class]])
        return [NSNumber numberWithFloat:[value floatValue]];
    
    else return [NSNumber numberWithFloat:0];
}

- (CGFloat)objectFormatFloatForKey:(id)aKey
{
    NSNumber *value = [self objectFormatFloatNumberForKey:aKey];
    return [value floatValue];
}

- (NSString*)objectFormatFloatStringForKey:(id)aKey retain:(int)digit
{
    CGFloat floatValue = [self objectFormatFloatForKey:aKey];
    NSString *format = [NSString stringWithFormat:@"%%0.%df",digit];
    return [NSString stringWithFormat:format,floatValue];
}

- (NSInteger)objectFormatIntegerForKey:(id)aKey
{
    id value = [self objectFormatForKey:aKey];
    if (value && [value isKindOfClass:[NSNumber class]])
        return [value integerValue];
    
    if (value && [value isKindOfClass:[NSString class]])
        return [value integerValue];
    
    else return 0;
}

- (NSNumber*)objectFormatIntegerNumberForKey:(id)aKey
{
    NSInteger value = [self objectFormatIntegerForKey:aKey];
    return [NSNumber numberWithInteger:value];
}

- (NSString*)objectFormatIntStringForKey:(id)aKey
{
    NSInteger value = [self objectFormatIntegerForKey:aKey];
    return [NSString stringWithFormat:@"%ld",value];
}


- (void)setFormatObject:(id)anObject forKey:(id <NSCopying>)aKey
{
    if (!anObject) anObject = @"";
    [self setObject:anObject forKey:aKey];
}

- (void)setFormatObject:(id)anObject forKey:(id <NSCopying>)aKey defaults:(id)value
{
    if (!anObject) anObject = value;
    if (!anObject) anObject = @"";
    [self setObject:anObject forKey:aKey];
}

#pragma mark 其他延伸方法

- (NSString*)objectFormatStringForKey:(id)aKey format:(NSString*)format defaults:(NSString*)defaultValue
{
    id value = [self objectFormatForKey:aKey];
    if (value) {
        NSString *valueString = [NSString stringWithFormat:@"%@",value];
        if (valueString.length > 0) return [NSString stringWithFormat:format,value];
        else return defaultValue;
    } else {
        return defaultValue;
    }
}

- (NSString*)objectFormatStringForKey:(id)aKey defaults:(NSString*)defaultValue
{
    id value = [self objectFormatForKey:aKey];
    if (value) {
        NSString *valueString = [NSString stringWithFormat:@"%@",value];
        return (valueString.length > 0) ? valueString : defaultValue;
    } else {
        return defaultValue;
    }
}

- (NSString*)objectFormatFloatStringForKey:(id)aKey retain:(int)digit format:(NSString*)format
{
    NSString *value = [self objectFormatFloatStringForKey:aKey retain:digit];
    if (format && format.length >0) return [NSString stringWithFormat:format,value];
    else return value;
}

@end
